<!-- page heading start-->
<div class="page-heading">
    <h3>
        部门列表
    </h3>

</div>
<!-- page heading end-->
<div class="row">
    <div class="col-sm-12">
        <section class="panel">
            <header class="panel-heading">
                <button class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                    增加
                </button>
            </header>
            <div class="panel-body">
                <table class="table table-hover general-table table-bordered">
                    <thead>
                    <tr>
                        <th width="20%">名称</th>
                        <th width="20%">负责人</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach($list as $val){?>
                    <tr>
                        <td>
                            <span style="word-break:break-all;word-wrap:break-word">
                                <?php echo $val['name'];?>
                            </span>
                        </td>
                        <td>
                            <span style="word-break:break-all;word-wrap:break-word">
                                <?php echo $val['leader'];?>
                            </span>
                        </td>
                        <td>
                            <div>
                                <button class="btn btn-primary" onclick="return show(this)" data-id="<?php echo $val['id'];?>"
                                    data-name="<?php echo $val['name'];?>">
                                    编辑
                                </button>
                                <a class="btn btn-danger" href="#"
                                   onclick="<?php echo 'return delete_data(' . $val['id'] .')';?>">删除</a>
                            </div>
                        </td>
                    </tr>
                    <?php }?>

                    </tbody>
                </table>
                <div class="pages">
                    {$page}
                </div>
            </div>
        </section>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <input type="hidden" id="id" value="<?php $val=(!empty($dept)) ?
                        $dept['id'] : ''; echo $val;?>">
                <div class="row">
                    <label class="form-inline">名称:
                    <input type="text" class="form-control" style="width: 300px;" name="name" value="<?php $val=(!empty($dept)) ?
                        $dept['name'] : ''; echo $val;?>" id="name"></label>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="update();">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
    function update() {
        var id = $("#id").val();
        var name = $("#name").val();
        $.ajax({
            data:{id:id, name:name},
            dataType:'json',
            url:'/User/Tool/deptUpdate',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        })
    }
    $("#myModal").on('hide.bs.modal',function (){
        $("#id").val("");
        $("#name").val("");
    });
    function show(e){
        $("#id").val($(e).attr("data-id"));
        $("#name").val($(e).attr("data-name"));
        $('#myModal').modal('show');
    }
    function delete_data(id){
        $.ajax({
            data:{id:id},
            dataType:'json',
            url:'/User/Tool/deptDelete',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
</script>